import { defineStore } from 'pinia'
import {getCookies} from '../utils/auth.js'

export const fileStore = defineStore('file', {
  state: () => {
    return {
      breadList: [],
      token: getCookies('token'),
      url: null
    }
  },
  actions: {
    change_Bread(fileObject) {
      this.breadList.forEach((item, index) => {
        if(item.pid === fileObject.pid) {
          this.breadList.splice(index, this.breadList.length - index)
          fileObject.name = item.name
        }
      })
      this.breadList.push(fileObject)
      this.breadList = Array.from(new Set(this.breadList))
    },
    save_Url(url) {
      this.url = url
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        storage: localStorage,
        path: ['breadList']
      }
    ]
  }
})


export const newsStore = defineStore('news', {
  state: () => {
    return {
      chatMenu: []
    }
  },
  actions: {
    save_ChatMenu(data) {
      const result = this.chatMenu.some(item => item.user_id === data.user_id)
      if(!result) {
        this.chatMenu.unshift(data)
      }
    },
    del_ChatMenu(data) {
      this.chatMenu.forEach((item,index) => {
        if(item.user_id === data) {
          this.chatMenu.splice(index, 1)
        }
      })
      console.log('删除的id')
      console.log(data)
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        storage: localStorage,
        path: ['chatMenu']
      }
    ]
  }
})
